package org.springcloud.wisdom.spring_02_classroom.teacher.tschedule.service;

import com.alibaba.fastjson.JSON;
import org.springcloud.wisdom.spring_02_classroom.admin.createcourse.entity.Course;
import org.springcloud.wisdom.spring_02_classroom.bean.Page;
import org.springcloud.wisdom.spring_02_classroom.student.sschedule.entity.Student;
import org.springcloud.wisdom.spring_02_classroom.teacher.tschedule.dao.TeacherScheduleDao;
import org.springcloud.wisdom.spring_02_classroom.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class TeacherScheduleService {
    @Autowired
    private TeacherScheduleDao teacherScheduleDao;
    @Autowired
    private WebSocketServer webSocketServer;

    //教师查看自己名下的课程
    public List<Course> findCoursesByTid(String tid) {
        if (tid == null || tid.trim().length() == 0){
            return null;
        }
        return teacherScheduleDao.selectCoursesByTid(tid);
    }

    // 教师查看所属课程的学生
    public List<Student> getStudentsByTid(String tid, String cid) {
        if (tid == null || tid.trim().length() == 0){
            return null;
        }if (cid == null || cid.trim().length() == 0){
            return null;
        }
        return teacherScheduleDao.selectCourseStudent(tid,cid);
    }

    // 教师随机选择一个学生
    public Student getRandomStudent(List<Student> students) {
        if (students == null || students.isEmpty()) {
            return null;
        }
        Random random = new Random();
        return students.get(random.nextInt(students.size()));
    }

    //课程历史
    public Page<Course> getcoursehistory(int pageNum, int pageSize, String tid  ){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Course> list= teacherScheduleDao.getcoursehistory(start,pageSize,tid);
        //获取总条目数
        int total= teacherScheduleDao.Countcoursehistory(tid);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Course> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
}